<?php

class NewsController extends Controller
{
	public function actionIndex()
	{
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A"';

		$dataProvider = new CActiveDataProvider('News',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC, updatedate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('index', array(
			'dataProvider'=>$dataProvider
		));
	}

	public function actionView($slug)
	{
		$this->layout = '//layouts/homeNewsView';
		$model = News::model()->findByAttributes(array('slug'=>$slug));

		if (empty($model)) {
			throw new CHttpException(404,'The requested page does not exist.');
		}

		$model->content = str_replace('<img src="/images/news/', '<img src="' . IMAGES_URL . 'news/', $model->content);
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A" AND id!=' . $model->id;
		$criteria->order = 'posteddate DESC';
		$criteria->limit = 5;
		$news = News::model()->findAll($criteria);

		$this->render('view', array(
			'model'=>$model,
			'news'=>$news
		));
	}
}